Completed
Pull Request — master (#434)
by
unknown
02:15
created

LivePreview.startLivePreview   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.5806
c 0
b 0
f 0
cc 4
nc 3
nop 2
1
/**
2
 * Nextcloud - Gallery
3
 *
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author François Sylvestre <[email protected]>
9
 *
10
 * @copyright François Sylvestre 2017
11
 */
12
/* global SlideShow, bigshot*/
0 ignored issues
show
Unused Code introduced by
bigshot does not seem to be used.
Loading history...
13
(function ($, SlideShow, LivePhotosKit, OC) {
14
	"use strict";
15
	/**
16
	 * Creates a zoomable preview
17
	 *
18
	 * @param {*} container
19
	 * @constructor
20
	 */
21
	var LivePreview = function (container) {
22
		this.container = container;
23
		this.element = this.container.get(0);
24
		this.livePhotoContainer = container.find('.livePhotoContainer');
25
		this.livePhotoContainer.css({display: 'block', width: '1px', height: '1px'});
26
		this.player = LivePhotosKit.createPlayer();
27
28
		this.livePhotoContainer.append(this.player);
29
		// this.livePhotoContainer.css('display', 'none');
30
31
		this._detectFullscreen();
32
		this._setupControls();
33
34
		// Reset image position and size on orientation change
35
		var self = this;
36
		$(window).bind('orientationchange resize', function () {
37
			self._resetView();
38
		});
39
	};
40
41
	LivePreview.prototype = {
42
		container: null,
43
		element: null,
44
		fullScreen: null,
45
		currentImage: null,
46
		mimeType: null,
47
		smallImageDimension: 200 / window.devicePixelRatio,
48
		smallImageScale: 2,
49
50
		/**
51
		 * Launches the Bigshot zoomable preview
52
		 *
53
		 * @param {*} image
54
		 * @param {number} currentImage
55
		 * @param {string} mimeType
0 ignored issues
show
Documentation introduced by
The parameter mimeType does not exist. Did you maybe forget to remove this comment?
Loading history...
56
		 */
57
		startLivePreview: function (image, currentImage) {
58
			var defer = $.Deferred();
59
			if (image.mimeType === "image/jpeg" && image.name.substr(-5).toLowerCase().indexOf('.jpg') !== -1) {
60
				var videoExt = '.mov';
61
				if (image.name.substr(-4) === '.JPG')
62
					videoExt = '.MOV';
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
Bug introduced by
{ was expected, but instead videoExt was given.
Loading history...
63
				var videoUrl = OC.generateUrl(['../remote.php/webdav/', encodeURI(image.path.substr(0, image.path.length - 4) + videoExt)].join(''));
0 ignored issues
show
Coding Style introduced by
Line is too long.
Loading history...
64
				$.ajax({
65
					url: videoUrl,
66
					type: 'HEAD',
67
					success: function() {
68
						this.livePhotoContainer.css({display: 'block'});
69
						this.currentImage = currentImage;
70
						this.mimeType = image.mimeType;
71
72
						this._resetView();
73
74
						this.player.photoSrc = this.currentImage.src;
75
						this.player.videoSrc = videoUrl;
76
						defer.resolve();
77
					}.bind(this),
78
					error: function() {
79
						this.livePhotoContainer.css('display', 'none');
80
						defer.reject();
81
					}.bind(this)
82
				});
83
			} else {
84
				defer.reject();
85
			}
86
			return defer.promise();
87
		},
88
89
		/**
90
		 * Resets the element for the next image to be displayed
91
		 */
92
		reset: function () {
93
			this.livePhotoContainer.css('display', 'none');
94
			this.player.photoSrc = null;
95
			this.player.videoSrc = null;
96
		},
97
98
		/**
99
		 * Throws away the zoomable preview
100
		 */
101
		stop: function () {
102
			if (this.fullScreen !== null) {
103
				this._fullScreenExit();
104
			}
105
		},
106
107
		/**
108
		 * Launches fullscreen mode if the browser supports it
109
		 */
110
		fullScreenToggle: function () {
111
			if (this.zoomable === null) {
112
				return;
113
			}
114
			if (this.fullScreen !== null) {
115
				this._fullScreenExit();
116
			} else {
117
				this._fullScreenStart();
118
			}
119
		},
120
121
		/**
122
		 * Detect fullscreen capability
123
		 * @private
124
		 */
125
		_detectFullscreen: function () {
126
			this.canFullScreen = this.element.requestFullscreen !== undefined ||
127
				this.element.mozRequestFullScreen !== undefined ||
128
				this.element.webkitRequestFullscreen !== undefined ||
129
				this.element.msRequestFullscreen !== undefined;
130
		},
131
132
		/**
133
		 * Makes UI controls work on touchscreens. Pinch only works on iOS
134
		 * @private
135
		 */
136
		_setupControls: function () {
137
			this.player.playbackStyle = LivePhotosKit.PlaybackStyle.FULL;
138
		},
139
140
		/**
141
		 * Resets the image to its original zoomed size
142
		 *
143
		 * @private
144
		 */
145
		_resetView: function () {
146
			var imgWidth = this.currentImage.naturalWidth / window.devicePixelRatio;
147
			var imgHeight = this.currentImage.naturalHeight / window.devicePixelRatio;
148
149
			var origSizeW = imgWidth;
150
			var origSizeH = imgHeight;
151
			var ratioVt=(origSizeW/origSizeH);
152
			var ratioHz=(origSizeH/origSizeW);
153
			var winW = $(window).width();
154
			var winH = $(window).height();
155
			var screenSizeW=Math.round(winW);
156
			var screenSizeH=Math.round(winH);
157
			var wantedWidth, wantedHeight, wantedLeft, wantedTop;
158
159
			if (origSizeW>=origSizeH) {
160
				var newHeight = Math.round(screenSizeW*ratioHz);
161
				if (newHeight <= screenSizeH){
162
					wantedHeight = newHeight;
163
					wantedWidth = screenSizeW;
164
				} else{
165
					wantedHeight = screenSizeH;
166
					wantedWidth = Math.round(screenSizeH*ratioVt);
167
				}
168
			} else{
169
				wantedHeight = screenSizeH;
170
				wantedWidth = Math.round(screenSizeH*ratioVt);
171
			}
172
			wantedLeft = Math.round((screenSizeW - wantedWidth) / 2);
173
			wantedTop = Math.round((screenSizeH - wantedHeight) / 2);
174
175
			$(this.livePhotoContainer.children().get(0)).css({'width': wantedWidth + 'px', 'height': wantedHeight + 'px', 'top': wantedTop + 'px', 'left': wantedLeft + 'px'});
0 ignored issues
show
Coding Style introduced by
Line is too long.
Loading history...
176
		},
177
178
		/**
179
		 * Starts the fullscreen previews
180
		 *
181
		 * @private
182
		 */
183
		_fullScreenStart: function () {
184
			this._resetView();
185
		},
186
187
		/**
188
		 * Stops the fullscreen previews
189
		 *
190
		 * @private
191
		 */
192
		_fullScreenExit: function () {
193
			this._resetView();
194
		}
195
	};
196
197
	SlideShow.LivePreview = LivePreview;
198
})(jQuery, SlideShow, LivePhotosKit, OC);
0 ignored issues
show
Bug introduced by
The variable LivePhotosKit seems to be never declared. If this is a global, consider adding a /** global: LivePhotosKit */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Bug introduced by
LivePhotosKit does not seem to be defined.
Loading history...
199